package com.example.mybatisplus.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.*;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus.mapper.ConRecordMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用来记录每个学生的咨询记录 服务实现类
 * </p>
 *
 * @author cft
 * @since 2022-06-24
 */
@Service
public class ConRecordServiceImpl extends ServiceImpl<ConRecordMapper, ConRecord> implements ConRecordService {


    /*
    * author:tqq
    * 实现咨询助理安排学生咨询，将咨询的时间、地点、老师id、初访id、、学生id加入咨询记录表
    * */

    @Autowired
    UserAccountService userAccountService;
    @Autowired
    FirstApoStatusService firstApoStatusService;
    @Autowired
    FirstAppointmentService firstAppointmentService;
    @Autowired
    ConRecordMapper conRecordMapper;
    @Autowired
    StuInfoService stuInfoService;
    @Override
    public void arrangConForStu(  ConRecord conRecord,FirstApoStatus firstApoStatus, UserAccount user) {
        conRecord.setFViewId(firstApoStatus.getFViewId()).
                setConStuId(firstAppointmentService.getById(firstApoStatus.getFApoId()).getStuId()).
                setConTeacherId(userAccountService.selectByName(user.getName()).getUserId());
        this.save(conRecord);

        firstApoStatus.setFResult("已安排咨询");
        QueryWrapper<FirstApoStatus> wrapper =new QueryWrapper<>();
        wrapper.eq("f_view_id",firstApoStatus.getFViewId());
        firstApoStatusService.update(firstApoStatus,wrapper);
    }

    /*
     * author:tqq
     * 显示咨询助理已安排的咨询
     * */
    @Override
    public Page<ConRecord> displayArranged(PageDTO pageDTO) {
        Page<ConRecord> page = new Page<>(pageDTO.getPageNo(),pageDTO.getPageSize());
		QueryWrapper<ConRecord> wrapper = new QueryWrapper<>();
		wrapper.eq("con_stu_times", 0);
        page = super.page(page,wrapper);
        return page;
    }

    /*
     * author:tqq
     * 查询所有的结案报告
     * */
    @Override
    public Page<ConRecord> displayEnd(PageDTO pageDTO) {
        Page<ConRecord> page = new Page<>(pageDTO.getPageNo(),pageDTO.getPageSize());
        QueryWrapper<ConRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("con_result","结案");
        page = super.page(page,wrapper);
        return page;
    }

    /*
     * author:tqq
     * 导出所有的结案报告
     * */
    @Override
    public void exportEndcon(HttpServletResponse httpServletResponse) {
        httpServletResponse.setContentType("application/vnd.ms-excel");
        httpServletResponse.setHeader("Content-Disposition","attachment; filename=结案报告汇总.xls");

        QueryWrapper<ConRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("con_result","结案");
        List<ConRecord> conlist = conRecordMapper.selectList(wrapper);
        List<UserAccount> teacherlist = new ArrayList<>();
        List<StuInfo> stulist = new ArrayList<>();
        List<FirstAppointment> firstApolist = new ArrayList<>();

        for (int i = 0; i < conlist.size(); i++) {
            teacherlist.add(userAccountService.getById(conlist.get(i).getConTeacherId()));
            stulist.add(stuInfoService.getById(conlist.get(i).getConStuId()));
            firstApolist.add(firstAppointmentService.getByoneStuId(conlist.get(i).getConStuId()));
        }
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet();

        Row row = sheet.createRow(0);

        Cell cell = row.createCell(0);
        cell.setCellValue("学生姓名");

        cell = row.createCell(1);
        cell.setCellValue("性别");

        cell = row.createCell(2);
        cell.setCellValue("学院");

        cell = row.createCell(3);
        cell.setCellValue("电子邮箱");

        cell = row.createCell(4);
        cell.setCellValue("咨询师姓名");

        cell = row.createCell(5);
        cell.setCellValue("咨询次数");

        cell = row.createCell(6);
        cell.setCellValue("咨询主题");

        cell = row.createCell(7);
        cell.setCellValue("咨询效果自评");

        for (int i = 0; i < conlist.size(); i++) {
            row =sheet.createRow(i+1);

            cell = row.createCell(0);
            cell.setCellValue(stulist.get(i).getName());

            cell = row.createCell(1);
            cell.setCellValue(stulist.get(i).getGender());

            cell = row.createCell(2);
            cell.setCellValue(stulist.get(i).getCollege());

            cell = row.createCell(3);
            cell.setCellValue(stulist.get(i).getEmail());

            cell = row.createCell(4);
            cell.setCellValue(teacherlist.get(i).getName());

            cell = row.createCell(5);
            cell.setCellValue(conlist.get(i).getConStuTimes());

            cell = row.createCell(6);
            cell.setCellValue(firstApolist.get(i).getApoTopic());

            cell = row.createCell(7);
            cell.setCellValue(conlist.get(i).getConDescription());

        }

        try {
            wb.write(httpServletResponse.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


	/*
	 * author:cft
	 * function:获得学生所有的咨询记录
	 */
	@Autowired
	ConRecordService conRecordService;
	@Override
	public List<ConRecord> getStuConDetail(Long stuId, Long conId) {
		QueryWrapper<ConRecord> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("con_stu_id", stuId).eq("con_teacher_id", conId);
		return conRecordService.list(queryWrapper);
	}


	/*
	 * author:cft
	 * function:先获得学生当前最大已咨询次数,再加一
	 */
	@Override
	public void setNewConRecordTimes(ConRecord conRecord) {
		QueryWrapper<ConRecord> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("con_teacher_id", conRecord.getConTeacherId()).eq("con_stu_id", conRecord.getConStuId());
		queryWrapper.select("max(con_stu_times) as mTimes");
		Map<String,Object> map=conRecordService.getMap(queryWrapper);
		int i=(Integer)map.get("mTimes");
		conRecord.setConStuTimes(i + 1);
		conRecordService.save(conRecord);
		//如果是结案记录,将该生之前所有的is_end标志设为1,同时前端禁止编辑
		if (conRecord.getIsEnd() == 1) {
			conRecordMapper.updateIsEnd(conRecord);

		}
	}

    /*
     * author:tqq
     * 查询当前咨询师的所有结案报告
     * */
    @Override
    public Page<ConRecord> displaymyEnd(PageDTO pageDTO) {
        Page<ConRecord> page = new Page<>(pageDTO.getPageNo(),pageDTO.getPageSize());
        QueryWrapper<ConRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("con_result","结案").eq("con_teacher_id", SessionUtils.getCurrentUserInfo().getUserId());
        page = super.page(page,wrapper);
        return page;
    }

    /*
     * author:tqq
     * 导出咨询师工作统计
     * */
    @Override
    public void exportConwork(HttpServletResponse httpServletResponse, JSONObject[] json) {
        httpServletResponse.setContentType("application/vnd.ms-excel");
        httpServletResponse.setHeader("Content-Disposition","attachment; filename=咨询师工作分析汇总.xls");



        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet();

        Row row = sheet.createRow(0);

        Cell cell = row.createCell(0);
        cell.setCellValue("咨询师id");

        cell = row.createCell(1);
        cell.setCellValue("姓名");

        cell = row.createCell(2);
        cell.setCellValue("咨询次数");

        cell = row.createCell(3);
        cell.setCellValue("咨询人次");


        for (int i = 0; i < json.length; i++) {
            row =sheet.createRow(i+1);

            cell = row.createCell(0);
            cell.setCellValue(json[i].getString("id1"));

            cell = row.createCell(1);
            cell.setCellValue(json[i].getString("name"));

            cell = row.createCell(2);
            cell.setCellValue(json[i].getString("conTimes"));

            cell = row.createCell(3);
            cell.setCellValue(json[i].getString("peoNum"));

        }

        try {
            wb.write(httpServletResponse.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
